package cn.aws360.boot.system.starter.controller;

import cn.aws360.boot.base.api.annotation.OpsLog;
import cn.aws360.boot.base.api.constant.enums.OpsTypeEnum;
import cn.aws360.boot.base.api.pojo.response.ResultModel;
import cn.aws360.boot.base.api.utils.ServletUtils;
import cn.aws360.boot.base.api.validation.ValidationGroup;
import cn.aws360.boot.data.api.pojo.request.PageQuery;
import cn.aws360.boot.data.api.pojo.response.PageResult;
import cn.aws360.boot.system.api.pojo.request.SysDictItemQuery;
import cn.aws360.boot.system.api.pojo.request.SysDictItemWrite;
import cn.aws360.boot.system.api.pojo.request.SysDictQuery;
import cn.aws360.boot.system.api.pojo.request.SysDictWrite;
import cn.aws360.boot.system.api.pojo.response.SysDictItemModel;
import cn.aws360.boot.system.api.pojo.response.SysDictModel;
import cn.aws360.boot.system.biz.service.SysDictItemService;
import cn.aws360.boot.system.biz.service.SysDictService;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static cn.aws360.boot.system.api.constant.SysConstant.*;

/**
 * @author junnan
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/dict")
@ApiSupport(order = MODULE_DICT_CODE)
@Api(value = MODULE_DICT_NAME, tags = MODULE_DICT_TAGS)
public class SysDictController {

    private final SysDictService dictService;
    private final SysDictItemService dictItemService;

    @GetMapping("/index.json")
    @SaCheckPermission("sys:dict:query")
    @ApiOperation(value = "字典页面", hidden = true)
    public ResultModel<JSONObject> index() {
        return ResultModel.ok(ServletUtils.getPageJson());
    }

    @GetMapping("/list")
    @SaCheckPermission("sys:dict:query")
    @ApiOperation(value = "字典列表")
    @ApiOperationSupport(order = 1)
    public ResultModel<List<SysDictModel>> list(SysDictQuery sysDictQuery) {
        return ResultModel.ok(dictService.findList(sysDictQuery));
    }

    @GetMapping("/page")
    @SaCheckPermission("sys:dict:query")
    @ApiOperation(value = "字典分页")
    @ApiOperationSupport(order = 1)
    public ResultModel<PageResult<SysDictModel>> page(SysDictQuery sysDictQuery, PageQuery pageQuery) {
        return ResultModel.ok(dictService.findPage(sysDictQuery, pageQuery));
    }

    @GetMapping("/{id}")
    @SaCheckPermission("sys:dict:detail")
    @ApiOperation(value = "字典详情")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParam(name = "id", value = "字典 id", dataTypeClass = Integer.class, required = true)
    public ResultModel<SysDictModel> detail(@PathVariable Integer id) {
        return ResultModel.ok(dictService.detail(id));
    }

    @PostMapping
    @SaCheckPermission("sys:dict:add")
    @OpsLog(title = "新增字典", opsType = OpsTypeEnum.INSERT)
    @ApiOperation(value = "新增字典")
    @ApiOperationSupport(order = 2)
    public ResultModel<Boolean> add(@Validated(ValidationGroup.add.class) @RequestBody SysDictWrite sysDictWrite) {
        return ResultModel.ok(dictService.add(sysDictWrite));
    }

    @PutMapping()
    @SaCheckPermission("sys:dict:edit")
    @OpsLog(title = "修改字典", opsType = OpsTypeEnum.UPDATE)
    @ApiOperation(value = "修改字典")
    @ApiOperationSupport(order = 3)
    public ResultModel<Boolean> edit(@Validated(ValidationGroup.edit.class) @RequestBody SysDictWrite sysDictWrite) {
        return ResultModel.ok(dictService.edit(sysDictWrite));
    }

    @DeleteMapping("/{ids}")
    @SaCheckPermission("sys:dict:remove")
    @OpsLog(title = "移除字典", opsType = OpsTypeEnum.REMOVE)
    @ApiOperation(value = "移除字典")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParam(name = "ids", value = "字典 ids", dataTypeClass = String.class, required = true)
    public ResultModel<Boolean> remove(@PathVariable String ids) {
        return ResultModel.ok(dictService.remove(ids));
    }

    @GetMapping("/item/list")
    @SaCheckPermission("sys:dict:query")
    @ApiOperation(value = "子项列表")
    @ApiOperationSupport(order = 11)
    public ResultModel<List<SysDictItemModel>> itemList(SysDictItemQuery sysDictItemQuery) {
        return ResultModel.ok(dictItemService.findList(sysDictItemQuery));
    }

    @GetMapping("/item/page")
    @SaCheckPermission("sys:dict:query")
    @ApiOperation(value = "子项分页")
    @ApiOperationSupport(order = 11)
    public ResultModel<PageResult<SysDictItemModel>> itemPage(SysDictItemQuery sysDictItemQuery, PageQuery pageQuery) {
        return ResultModel.ok(dictItemService.findPage(sysDictItemQuery, pageQuery));
    }

    @GetMapping("/item/{id}")
    @SaCheckPermission("sys:dict:detail")
    @ApiOperation(value = "子项详情")
    @ApiOperationSupport(order = 11)
    @ApiImplicitParam(name = "id", value = "子项 id", dataTypeClass = Integer.class, required = true)
    public ResultModel<SysDictItemModel> itemDetail(@PathVariable Integer id) {
        return ResultModel.ok(dictItemService.detail(id));
    }

    @PostMapping("/item")
    @SaCheckPermission("sys:dict:add")
    @OpsLog(title = "新增子项", opsType = OpsTypeEnum.INSERT)
    @ApiOperation(value = "新增子项")
    @ApiOperationSupport(order = 12)
    public ResultModel<Boolean> itemAdd(@Validated(ValidationGroup.add.class) @RequestBody SysDictItemWrite sysDictItemWrite) {
        return ResultModel.ok(dictItemService.add(sysDictItemWrite));
    }

    @PutMapping("/item")
    @SaCheckPermission("sys:dict:edit")
    @OpsLog(title = "修改子项", opsType = OpsTypeEnum.UPDATE)
    @ApiOperation(value = "修改子项")
    @ApiOperationSupport(order = 13)
    public ResultModel<Boolean> itemEdit(@Validated(ValidationGroup.edit.class) @RequestBody SysDictItemWrite sysDictItemWrite) {
        return ResultModel.ok(dictItemService.edit(sysDictItemWrite));
    }

    @DeleteMapping("/item/{ids}")
    @SaCheckPermission("sys:dict:remove")
    @OpsLog(title = "移除子项", opsType = OpsTypeEnum.REMOVE)
    @ApiOperation(value = "移除子项")
    @ApiOperationSupport(order = 14)
    @ApiImplicitParam(name = "ids", value = "子项 ids", dataTypeClass = String.class, required = true)
    public ResultModel<Boolean> itemRemove(@PathVariable String ids) {
        return ResultModel.ok(dictItemService.remove(ids));
    }

    @GetMapping("/refresh")
    @SaCheckPermission("sys:dict:query")
    @ApiOperation(value = "字典缓存刷新")
    @ApiOperationSupport(order = 20)
    public ResultModel<Boolean> refresh() {
        return ResultModel.ok(dictService.refreshCache());
    }

    @GetMapping("/item/sel/code/{code}")
    @ApiOperation(value = "根据字典编码获取子项列表")
    @ApiOperationSupport(order = 22)
    @ApiImplicitParam(name = "code", value = "字典 code", dataTypeClass = String.class, required = true)
    public ResultModel<List<SysDictItemModel>> itemSelByCode(@PathVariable String code) {
        return ResultModel.ok(dictService.findItemSelByCode(code));
    }

    @GetMapping("/item/map/code/{code}")
    @ApiOperation(value = "根据字典编码获取子项映射")
    @ApiOperationSupport(order = 21)
    @ApiImplicitParam(name = "code", value = "字典 code", dataTypeClass = String.class, required = true)
    public ResultModel<Map<Integer, String>> itemMapByCode(@PathVariable String code) {
        return ResultModel.ok(dictService.findItemMapByCode(code));
    }

}
